package xj.graph2d.app.prop;

import static xj.app.BaseApplicationFrame.getResourceString;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.Border;

import org.jdesktop.swingx.JXPanel;

import xj.graph2d.app.DrawApp;
import xj.model.uml.Changeability;
import xj.model.uml.CollectionType;
import xj.model.uml.Multiplicity;
import xj.model.uml.Navigability;
import xj.model.uml.UMLAssociationEnd;

public class UMLAssociationModelPane 
  extends UMLBasicModelPane {

  public UMLAssociationModelPane(DrawApp main) {
    this(main, false);
  }

  public UMLAssociationModelPane(DrawApp main, boolean popup) {
    super(main);

    associationName = new ModelPropertiesPane.NameField("name",	
							getResourceString("AssociationName.text"));
    addRow(associationName);

    Changeability[] ch = { 
      Changeability.Changeable, 
      Changeability.Constant,
      Changeability.Derived, 
      Changeability.AddOnly
    };

    UMLAssociationEnd.AggregationKind[] ag = {
      UMLAssociationEnd.AggregationKind.None,
      UMLAssociationEnd.AggregationKind.Shared,
      UMLAssociationEnd.AggregationKind.Composite
    };

    Navigability[] nav = { 
      Navigability.Default, 
      Navigability.Navigable,
      Navigability.NonNavigable
    };

    leftPanel = new GridPanel(!popup);
    rightPanel = new GridPanel(!popup);

    leftRole = new ModelPropertiesPane.NameField("leftRole", 
						 getResourceString("RoleName.text"));
    leftPanel.addRow(leftRole);
    leftMultiplicity = new MultiplicityField("leftMultiplicity", 
					     getResourceString("Multiplicity.text"));
    leftPanel.addRow(leftMultiplicity);
    leftCollection = new CollectionTypeField("leftCollectionType", 
					     getResourceString("Collection.text"));
    leftPanel.addRow(leftCollection);
    leftChangeability = new ModelPropertiesPane.ChoiceField("leftChangeability", 
							    getResourceString("Changeability.text"), ch);
    leftPanel.addRow(leftChangeability);
    leftAggregation = new ModelPropertiesPane.ChoiceField("leftAggregation", 
							  getResourceString("Aggregation.text"), ag);
    leftPanel.addRow(leftAggregation);
    navLeft = new ModelPropertiesPane.ChoiceField("navigabilityToLeft", 
						  getResourceString("Navigability.text"), nav);
    leftPanel.addRow(navLeft);

    rightRole = new ModelPropertiesPane.NameField("rightRole", 
						  getResourceString("RoleName.text"));
    rightPanel.addRow(rightRole);
    rightMultiplicity = new MultiplicityField("rightMultiplicity", 
					      getResourceString("Multiplicity.text"));
    rightPanel.addRow(rightMultiplicity);
    rightCollection = new CollectionTypeField("rightCollectionType", 
					      getResourceString("Collection.text"));
    rightPanel.addRow(rightCollection);
    rightChangeability = new ModelPropertiesPane.ChoiceField("rightChangeability", 
							     getResourceString("Changeability.text"), ch);
    rightPanel.addRow(rightChangeability);
    rightAggregation = new ModelPropertiesPane.ChoiceField("rightAggregation", 
							   getResourceString("Aggregation.text"), ag);
    rightPanel.addRow(rightAggregation);
    navRight = new ModelPropertiesPane.ChoiceField("navigabilityToRight", 
						   getResourceString("Navigability.text"), nav);
    rightPanel.addRow(navRight);

    ModelPropertyField[] fields = { 
      leftRole, 
      leftMultiplicity, 
      leftCollection,
      leftChangeability, 
      leftAggregation, 
      navLeft,

      rightRole, 
      rightMultiplicity, 
      rightCollection, 
      rightChangeability,
      rightAggregation, 
      navRight, 
    };
    addModelPropertyFields(fields);

    if (!popup) {
      JPanel endsPanel = new JPanel();
      /*
      TitledBorder border = 
	BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), 
					 getResourceString("AssociationEnds.text"));
      */
      Border border = BorderFactory.createEtchedBorder();
      endsPanel.setBorder(border);
      endsPanel.setLayout(new BorderLayout());
      JLabel title = new JLabel(getResourceString("AssociationEnds.text")); 
      endsPanel.add(title, BorderLayout.NORTH);
      JTabbedPane tabbedPane = new JTabbedPane();
      tabbedPane.add(leftPanel, getResourceString("End.text") + " 1");
      tabbedPane.add(rightPanel, getResourceString("End.text") + " 2");
      endsPanel.add(tabbedPane, BorderLayout.CENTER);
      addRow(endsPanel);

      addBasicFields();

      addFiller();
    } else { 
      /*
      TitledBorder border = 
	BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), 
					 getResourceString("AssociationEnd.text") + " 1");
      */
      Border border = BorderFactory.createEtchedBorder();
      leftTitlePanel = new JXPanel();
      leftTitlePanel.setBorder(border);
      leftTitlePanel.setLayout(new BorderLayout());
      JLabel leftTitle = new JLabel(getResourceString("AssociationEnd.text") + " 1"); 
      leftTitlePanel.add(leftTitle, BorderLayout.NORTH);
      leftTitlePanel.add(leftPanel, BorderLayout.CENTER);

      /*
      border = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), 
						getResourceString("AssociationEnd.text") + " 2");
      */
      border = BorderFactory.createEtchedBorder();
      rightTitlePanel = new JXPanel();
      rightTitlePanel.setBorder(border);
      rightTitlePanel.setLayout(new BorderLayout());
      JLabel rightTitle = new JLabel(getResourceString("AssociationEnd.text") + " 2"); 
      rightTitlePanel.add(rightTitle, BorderLayout.NORTH);
      rightTitlePanel.add(rightPanel, BorderLayout.CENTER);
    }
  }

  protected ModelPropertiesPane.NameField associationName;

  protected ModelPropertiesPane.NameField leftRole;

  protected MultiplicityField leftMultiplicity;

  protected ModelPropertiesPane.ChoiceField leftChangeability;

  protected ModelPropertiesPane.ChoiceField leftAggregation;

  protected ModelPropertiesPane.ChoiceField navLeft;

  protected CollectionTypeField leftCollection;

  protected ModelPropertiesPane.NameField rightRole;

  protected MultiplicityField rightMultiplicity;

  protected ModelPropertiesPane.ChoiceField rightChangeability;

  protected ModelPropertiesPane.ChoiceField rightAggregation;

  protected ModelPropertiesPane.ChoiceField navRight;

  protected CollectionTypeField rightCollection;

  protected GridPanel leftPanel;
  protected GridPanel rightPanel;

  protected JXPanel leftTitlePanel;
  protected JXPanel rightTitlePanel;

  public GridPanel getLeftPanel() { 
    return leftPanel;
  }

  public GridPanel getRightPanel() { 
    return rightPanel;
  }

  public JXPanel getLeftTitlePanel() { 
    return leftTitlePanel;
  }

  public JXPanel getRightTitlePanel() { 
    return rightTitlePanel;
  }

  static class MultiplicityField extends ModelPropertyField {

    MultiplicityField(String fieldName, String displayName) {
      super(fieldName, displayName);
      cbox = new JComboBox();
      cbox.addItem("");
      cbox.addItem(Multiplicity.toString(Multiplicity.One));
      cbox.addItem(Multiplicity.toString(Multiplicity.ZeroOrOne));
      cbox.addItem(Multiplicity.toString(Multiplicity.OneToMany));
      cbox.addItem(Multiplicity.toString(Multiplicity.Many));
      cbox.setEditable(true);
      cbox.setBackground(Color.white);
      //cbox.setFont(Style.font);
      cbox.addItemListener(new ItemListener() {
	public void itemStateChanged(ItemEvent e) {
	  if (e.getStateChange() == ItemEvent.SELECTED) {
	    updateModelValue();
	    updateShape();
	  }
	}
      });
      setEditor(cbox);
    }

    @Override
    public void updateEditorValue() {
      Object v = getModelValue();
      if (v instanceof Multiplicity) {
	cbox.setSelectedItem(Multiplicity.toString((Multiplicity) v));
      } else { 
	cbox.setSelectedItem("");
      }
    }

    @Override
    public void updateModelValue() {
      String v = (String) cbox.getSelectedItem();
      setModelValue(Multiplicity.toMultiplicity(v));
    }

    protected JComboBox cbox;

  }

  static class CollectionTypeField extends ModelPropertyField {

    CollectionTypeField(String fieldName, String displayName) {
      super(fieldName, displayName);
      cbox = new JComboBox();
      cbox.addItem("");
      cbox.addItem(CollectionType.toString(CollectionType.Set));
      cbox.addItem(CollectionType.toString(CollectionType.Bag));
      cbox.addItem(CollectionType.toString(CollectionType.List));
      cbox.addItem(CollectionType.toString(CollectionType.OrderedSet));
      cbox.setBackground(Color.white);
      //cbox.setFont(Style.font);
      cbox.addItemListener(new ItemListener() {
	public void itemStateChanged(ItemEvent e) {
	  if (e.getStateChange() == ItemEvent.SELECTED) {
	    updateModelValue();
	    updateShape();
	  }
	}
      });
      setEditor(cbox);
    }

    @Override
    public void updateEditorValue() {
      Object v = getModelValue();
      if (v instanceof CollectionType) {
	cbox.setSelectedItem(CollectionType.toString((CollectionType) v));
      } else { 
	cbox.setSelectedItem("");
      }
    }

    @Override
    public void updateModelValue() {
      String v = (String) cbox.getSelectedItem();
      setModelValue(CollectionType.toCollectionType(v));
    }

    protected JComboBox cbox;

  }

}
